const AppCategorySQL = {
    insert: 'INSERT INTO appcategory(name, bgimg) VALUES(?, ?);',
    update: 'UPDATE appcategory SET name=?, bgimg=? WHERE id=?;',
    delete: 'DELETE FROM appcategory WHERE id=?;',
    queryById: 'SELECT * FROM appcategory WHERE id=?;',
    queryAll: 'SELECT * FROM appcategory;'
}

const AppSQL = {
    insert: 'INSERT INTO app(cateid, name, timg, installpath, enable) VALUES(?, ?, ?, ?, ?);',
    update: 'UPDATE app SET cateid=?, name=?, timg=? installpath=? enable=? WHERE id=?;',
    delete: 'DELETE FROM app WHERE id=?;',
    queryById: 'SELECT * FROM app WHERE id=?;',
    queryByCateId: 'SELECT * FROM app WHERE cateid=?;',
    queryAll: 'SELECT * FROM app;'
}


var database = null

/**
 *  链接数据库
 */
function init() {
    if(database === null) {
        database = require('./vrdb').create(0);
    } else {
        console.warn('reset connect database.')
    }
}

/**
 * 断开数据库链接
 * @param {*} handle 
 */
function uninit() {
    if(database === null) {
        console.warn('The database not connect.')      
    } else {
        database.release()
        database = null
    }
}

function insertAppCategory(name, bgimg, callback) {
    if(database === null) { 
        console.error('AppCategory insert data failed. database is null')

        if(callback !== undefined && callback !== null)
            callback({result: false, message: 'the call before not call init()'})
    } else {
        database.insert(AppCategorySQL.insert, [name, bgimg], callback)
    }
}

function updateAppCategory(id, name, bgimg, callback) {
    if(database === null) { 
        console.error('AppCategory insert data failed. database is null')

        if(callback !== undefined && callback !== null)
            callback({result: false, message: 'the call before not call init()'})
    } else {
        database.update(AppCategorySQL.update, [name, bgimg, id], callback)
    }
}

function deleteAppCategory(id, callback) {
    if(database === null) { 
        console.error('AppCategory insert data failed. database is null')

        if(callback !== undefined && callback !== null)
            callback({result: false, message: 'the call before not call init()'})
    } else {
        database.delete(AppCategorySQL.delete, [id], callback)
    }
}

function queryAppCategoryById(id, callback) {
    if(database === null) { 
        console.error('AppCategory insert data failed. database is null')

        if(callback !== undefined && callback !== null)
            callback({result: false, message: 'the call before not call init()'})
    } else {
        database.select(AppCategorySQL.queryById, [id], callback)
    }
}

function queryAppCategoryAll(callback) {
    if(database === null) { 
        console.error('AppCategory insert data failed. database is null')

        if(callback !== undefined && callback !== null)
            callback({result: false, message: 'the call before not call init()'})
    } else {
        database.select(AppCategorySQL.queryAll, undefined, callback)
    }
}

function insertApp(cateid, name, timg, installpath, enable, callback) {
    if(database === null) { 
        console.error('AppCategory insert data failed. database is null')

        if(callback !== undefined && callback !== null)
            callback({result: false, message: 'the call before not call init()'})
    } else {
        database.insert(AppSQL.insert, [cateid, name, timg, installpath, enable], callback)
    }
}

function updateApp(id, cateid, name, timg, installpath, enable, callback) {
    if(database === null) { 
        console.error('AppCategory insert data failed. database is null')

        if(callback !== undefined && callback !== null)
            callback({result: false, message: 'the call before not call init()'})
    } else {
        database.update(AppSQL.update, [cateid, name, timg, installpath, enable, id], callback)
    }
}

function deleteApp(id, callback) {
    if(database === null) { 
        console.error('AppCategory insert data failed. database is null')

        if(callback !== undefined && callback !== null)
            callback({result: false, message: 'the call before not call init()'})
    } else {
        database.delete(AppSQL.delete, [id], callback)
    }
}

function queryAppById(id, callback) {
    if(database === null) { 
        console.error('AppCategory insert data failed. database is null')

        if(callback !== undefined && callback !== null)
            callback({result: false, message: 'the call before not call init()'})
    } else {
        database.select(AppSQL.queryById, [id], callback)
    }
}

function queryAppByCateId(cateid, callback) {
    if(database === null) { 
        console.error('AppCategory insert data failed. database is null')

        if(callback !== undefined && callback !== null)
            callback({result: false, message: 'the call before not call init()'})
    } else {
        database.select(AppSQL.queryByCateId, [cateid], callback)
    }
}

function queryAppAll(callback) {
    if(database === null) { 
        console.error('AppCategory insert data failed. database is null')

        if(callback !== undefined && callback !== null)
            callback({result: false, message: 'the call before not call init()'})
    } else {
        database.select(AppSQL.queryAll, undefined, callback)
    }
}

module.exports = {
    init,
    uninit,
    insertAppCategory,
    updateAppCategory,
    deleteAppCategory,
    queryAppCategoryById,
    queryAppCategoryAll,
    insertApp,
    updateApp,
    deleteApp,
    queryAppById,
    queryAppByCateId,
    queryAppAll
}